package jTorrent;

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.text.DecimalFormat;

public class Misc
{
	private static DecimalFormat df = new DecimalFormat("#.#");
	
	public static String getLongestRowColumn(Object[][] rows, Integer column)
	{
		String longest = "";
        for(int i=0; i < rows.length; i++)
        {
        	String str = String.valueOf(rows[i][column]);
            if(str.length() > longest.length())
            {
               longest = str;
            }
        }
		
		return longest;
	}
	
	/**
	 * Returns the file size with a fitting file size unit.
	 * @param fileSize
	 * @param transfer should be true if you need the "per second" after the fileUnit.
	 * @return String with file size and number
	 */
	public static String calcFittingFileUnit(Double fileSize, Boolean transfer)
	{
		String fileUnit = "B";
		final long[] Constants = {0, 1024, 1048575, 1073741824, 1099511627776L, 1125899906842624L, 1152921504606846976L};
		
		if(fileSize >= Constants[6])
		{ 
			fileSize /= Constants[6];
			fileUnit = "EiB";
		}
		else if(fileSize >= Constants[5])
		{
			fileSize /= Constants[5];
			fileUnit = "PiB";
		}
		else if(fileSize >= Constants[4])
		{
			fileSize /= Constants[4];
			fileUnit = "TiB";
		}
		else if(fileSize >= Constants[3])
		{
			fileSize /= Constants[3];
			fileUnit = "GiB";
		}
		else if(fileSize >= Constants[2])
		{
			fileSize /= Constants[2];
			fileUnit = "MiB";
		}
		else if(fileSize >= Constants[1])
		{
			fileSize /= Constants[1];
			fileUnit = "KiB";
		}
		
		fileSize = Math.floor(fileSize * 1e5) / 1e5;
		
		return df.format(fileSize) + " " + fileUnit + (transfer ? "/s" : "");
	}
	
	public static Image createStateIcon(Color color, Symbol symbol)
	{
		BufferedImage img = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2 = img.createGraphics();

		g2.setComposite(AlphaComposite.SrcOver);
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2.fillOval(0, 0, 16, 16);

		g2.setComposite(AlphaComposite.SrcAtop);
		g2.setPaint(color);
		g2.fillRect(0, 0, 16, 16);
		
		g2.setPaint(Color.white);
		g2.setComposite(AlphaComposite.SrcOver);
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
		Polygon[] polygons = symbol.getPolygon();
		for(Polygon polygon : polygons)
		{
			g2.fillPolygon(polygon);
		}		
		return img;
	}
	
	public static Image createConnectionIcon(Boolean connected)
	{
		BufferedImage img = new BufferedImage(23, 16, BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2 = img.createGraphics();
		
		g2.setComposite(AlphaComposite.SrcOver);
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2.fillOval(0, 0, 16, 16);
		
		g2.setComposite(AlphaComposite.SrcAtop);
		g2.setPaint(Color.BLACK);
		g2.fillRect(0, 0, 16, 16);
		
		g2.setComposite(AlphaComposite.Src);
		g2.setPaint(new Color(0, 0, 0, 0));
		g2.fillRect(0, 0, 8, 16);
		
		g2.setStroke(new BasicStroke(3));
		g2.setComposite(AlphaComposite.SrcOver);
		g2.setPaint(Color.BLACK);
		g2.drawLine(2, 5, 8, 5);
		
		g2.setStroke(new BasicStroke(3));
		g2.setPaint(Color.BLACK);
		g2.drawLine(2, 11, 8, 11);
		
		g2.setStroke(new BasicStroke(4.8f));
		g2.setPaint(Color.BLACK);
		g2.drawLine(17, 8, 22, 8);
		
		if(!connected)
		{
			g2.setStroke(new BasicStroke(2));
			g2.setPaint(Color.RED);
			g2.drawLine(1, 1, 21, 15);
			g2.drawLine(21, 1, 1 , 15);
		}
		
		g2.dispose();
		
		return img;
	}
}